package edu.zzuli.model.xt.jqqx.zzjggl.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import edu.zzuli.common.Pagination;
import edu.zzuli.common.TreeNode;
import edu.zzuli.model.dao.BaseDaoEntity;
import edu.zzuli.model.dao.BasePO;
import edu.zzuli.model.orm.po.GG_CZLB;
import edu.zzuli.model.orm.po.GG_CZYB;
import edu.zzuli.model.orm.po.GG_JGBH;
import edu.zzuli.model.orm.po.GG_LBFP;
import edu.zzuli.model.xt.jqqx.zzjggl.iface.ZzjgglService;
import edu.zzuli.util.PasswordEncoder;

/**
 * @author tianshaojie
 * @date 2011-8-17
 * @discription : 组织结构管理业务类
 */
@Transactional
@Service("zzjgglService")
public class ZzjgglServiceImpl implements ZzjgglService{
	
	@Resource
	private BaseDaoEntity baseDaoEntity;
	
	public BasePO selectSinglePO(BasePO basePO) {
		return this.baseDaoEntity.selectSingle(basePO, basePO.getStrKeyWhere());
	}
	
	public List selectSplit(String sql, Pagination pagination) {
		return this.baseDaoEntity.selectSplit(sql, pagination);
	}
	
	public List selectSplit(String sql, SqlParameterSource parameterSource, Pagination pagination) {
		return this.baseDaoEntity.selectSplit(sql, parameterSource, pagination);
	}
	
	/**
	 * @author tianshaojie
	 * @date 2011-8-17
	 * @discription : 根据登录管理员所在机构获得子机构树 
	 * @return List
	 */
	public List<TreeNode> getZzjgTree(String jgbh) {
		String sql = "select jgbh.jgbh, jgbh.sjjg, jgbh.jgmc, jgbh.jglb, SYS_CONNECT_BY_PATH(to_char(jgbh.sxh, '0000'), '.') sort"
			+ " from gg_jgbh jgbh"
			+ " start with jgbh.jgbh = :JGBH"
			+ " connect by jgbh.sjjg = prior jgbh.jgbh"
			+ " order by sort";
		Map paramMap = new HashMap();
		paramMap.put("JGBH", jgbh);
		List<Map<String, Object>> listMap = this.baseDaoEntity.queryForList(sql,paramMap);
		List<TreeNode> listTreeNode = new ArrayList<TreeNode>();
		for(Map map:listMap) {
			String id = (String)map.get("JGBH");
			TreeNode node = new TreeNode(id,(String)map.get("JGMC"),(String)map.get("SJJG"));
			listTreeNode.add(node);
		}
		return listTreeNode;
	}
	
	/**
	 * @author tianshaojie
	 * @date 2011-8-17
	 * @discription : 添加修改机构人员
	 * @param gg_czyb void
	 */
	public void saveOrUpdateGG_CZYB(GG_CZYB gg_czyb) {
		if (gg_czyb.getId() != null && !"".equals(gg_czyb.getId())) {
			this.baseDaoEntity.getHibernateDao().update(gg_czyb,gg_czyb.getStrKeyWhere());
		} else {
			int sxh = this.baseDaoEntity.getJdbcTemplate().queryForInt("select nvl(max(sxh),0)+1 sxh from gg_czyb where jgbh = '" + gg_czyb.getGG_JGBH().getJgbh() + "'");
			gg_czyb.setSxh(sxh);
			gg_czyb.setSybz(1);
			gg_czyb.setMm(PasswordEncoder.encode("0"));
			this.baseDaoEntity.save(gg_czyb);
		}
	}
	
	/**
	 * @author tianshaojie
	 * @date 2011-8-17
	 * @discription : 删除机构人员
	 * @param ids void
	 */
	public void deleteGG_CZYB(String ids) {
		if (ids != null && !"".equals(ids)) {
			String[] idsArr = ids.split(",");
			for (String id : idsArr) {
				this.baseDaoEntity.getHibernateDao().delete(new GG_LBFP(), new StringBuffer(" fpid = id"));
				this.baseDaoEntity.getHibernateDao().delete(new GG_CZYB(id));
				
			}
		}
	}

	/**
	 * @author tianshaojie
	 * @date 2011-8-21
	 * @discription : 添加修改组织结构
	 * @param gg_jgbh void
	 */
	public void saveOrUpdateGG_JGBH(GG_JGBH gg_jgbh) {
		if (gg_jgbh.getJgbh() != null && gg_jgbh.getJgbh().length() > 0) {
			this.baseDaoEntity.getHibernateDao().update(gg_jgbh, gg_jgbh.getStrKeyWhere());
		} else {
			String sql = "select nvl(max(sxh),0)+1 sxh from gg_jgbh where sjjg = ?";
			int sxh = this.baseDaoEntity.getJdbcTemplate().queryForInt(sql, gg_jgbh.getGG_JGBH().getJgbh());
			GG_JGBH	sjjg = (GG_JGBH) this.baseDaoEntity.selectSingle(gg_jgbh.getGG_JGBH());
			gg_jgbh.setJglb(sjjg.getJglb());
			gg_jgbh.setSxh(sxh);
			this.baseDaoEntity.getHibernateDao().save(gg_jgbh);
		}
	}
	
	/**
	 * @author tianshaojie
	 * @date 2011-8-21
	 * @discription : 删除组织机构
	 * @param jgbh void
	 */
	public void deleteGG_JGBH(String jgbh) {
		if (getJgryCount(jgbh) > 0) {
			String delLbfp = "delete from gg_lbfp where fpid in(select id from gg_czyb where jgbh = ?)";
			this.baseDaoEntity.getJdbcTemplate().update(delLbfp,new Object[] {jgbh});
			this.baseDaoEntity.getHibernateDao().delete(new GG_CZYB(), new StringBuffer( "jgbh = '" + jgbh + "'"));
		}
		this.baseDaoEntity.getHibernateDao().delete(new GG_JGBH(jgbh));
	}
	
	/**
	 * @author tianshaojie
	 * @date 2011-8-21
	 * @discription : 获得机构下机构人员数量
	 * @param jgbh void
	 */
	public int getJgryCount(String jgbh) {
		String sql = "select count(*) from gg_czyb where jgbh = ?";
		Map map = new HashMap();
		map.put("JGBH", jgbh);
		return this.baseDaoEntity.getJdbcTemplate().queryForInt(sql, jgbh);
	}
	
	/**
	 * @author tianshaojie
	 * @date 2011-8-25
	 * @discription : 查询机构下的业务角色
	 * @param jglb
	 * @return List
	 */
	@SuppressWarnings("deprecation")
	public List getCzlbList(String jgbh,String czyid,List selectedCzlb) {
		Map paramMap = new HashMap();
		paramMap.put("JGBH", jgbh);
		paramMap.put("CZYID", czyid);
		
		String lbfpByJsSql = "select lbid from gg_lbfp where fpid = :CZYID";
		String lbfpByGwSql = "select lb.lbid from gg_czyb_gwlb gw, gg_lbfp lb where gw.gwid = lb.fpid and gw.czyid = :CZYID";
		selectedCzlb.addAll(this.baseDaoEntity.queryForList(lbfpByJsSql, paramMap));
		selectedCzlb.addAll(this.baseDaoEntity.queryForList(lbfpByGwSql, paramMap));
		
		String sql = "select id,lbmc,sm from gg_czlb where jgbh != '0001' order by id desc";
		return this.baseDaoEntity.queryForList(sql);
	}
	
	/**
	 * @author tianshaojie
	 * @date 2011-8-27
	 * @discription : 保存机构人员权限
	 */
	public void saveJgryQx(String czyid, String czlbIds) {
		this.baseDaoEntity.getHibernateDao().delete(new GG_LBFP(), new StringBuffer(" fpid = '" + czyid + "'"));
		if (czlbIds != null && czlbIds.length() > 0) {
			String[] czlbIdsArr = czlbIds.split(",");
			List gg_lbfpList = new ArrayList();
			for(String lbid : czlbIdsArr) {
				GG_LBFP gg_lbfp = new GG_LBFP();
				gg_lbfp.setFpid(czyid);
				gg_lbfp.setGG_CZLB(new GG_CZLB(lbid));
				gg_lbfpList.add(gg_lbfp);
			}
			this.baseDaoEntity.getHibernateDao().save(gg_lbfpList);
		}
	}
	
	/**
	 * @author tianshaojie
	 * @date 2011-8-27
	 * @discription : 验证登录号唯一性 
	 * @param czyid
	 * @param dlh
	 * @return boolean
	 */
	public boolean checkUniqueDlh(String czyid,String dlh) {
		String id = "";
		Map paramMap = new HashMap();
		paramMap.put("DLH", dlh);
		if (czyid != null && czyid.length() > 0) {
			paramMap.put("ID", czyid);
			String sql = "select id from gg_czyb where id != :ID and dlh = :DLH";
			id = this.baseDaoEntity.queryForString(sql, paramMap);
		} else {
			String sql = "select id from gg_czyb where dlh = :DLH";
			id = this.baseDaoEntity.queryForString(sql, paramMap);
		}
		if (id != null && id.length() > 0) {
			return false;
		} else {
			return true;
		}
	}
}
